﻿--[[
   @brief    control module for poptips ui
   @author   dragonqiu
   @version  2.0
   @date     2015-12-12
--]]

poptips_control =
{
    current_link_url = "",
    current_msg_id = "0",

    init = function()
        base_utility.log("[poptips_control] init");
        ierd_tgp.tpf_ui.Require("poptips\\model.lua");
        ierd_tgp.tpf_ui.Require("poptips\\view.lua");
        ierd_tgp.tpf_ui.Require("poptips\\server_tips_control.lua");
        ierd_tgp.tpf_ui.Require("poptips\\default_web_poptips.lua");
        ierd_tgp.tpf_ui.Require("local_web\\local_web_wnd.lua");
    end;

    close_tips = function()
        base_utility.log("[poptips_control] close_tips");
        local uilib = ui_utility.uilib();
        poptips_view.close(uilib);
    end;

    no_handle_close = function(win_id)
        base_utility.log("[poptips_control] close_tips");
        local uilib = ui_utility.uilib();
        poptips_view:_close_impl(uilib, win_id);
    end;

    new_close_tips = function(win_id)
        base_utility.log("[poptips_control] close_tips");
        local uilib = ui_utility.uilib();
        local close_btn_id = ui_utility.get_ctrl_id_by_path(win_id,
                "TitleBar1\\Btn_close");
        poptips_view:new_close(uilib, close_btn_id);
    end;

    check_need_feedback = function(param)
        if not param or nil == param then
            return param;
        end
        if nil == param.game_id or param.game_id <= 0 then
            return param;
        end
        if nil == param.error_code or param.error_code == "" then
            return param;
        end

        local game_id = param.game_id;
        local error_code = param.error_code;
        local error_code_info = comm_center.call_service("Svr_GetErrorInfoByNewErrorCode", { new_error_code_id = error_code });
        local error_code_desc = param.desc; -- 默认使用传进来的提示语，desc 或者 descTitle。这两个都有业务使用，不统一
        if nil == error_code_desc or error_code_desc == "" then
            error_code_desc = param.descTitle;
        end
        if nil ~= error_code_info and error_code_info.is_success and error_code_info.has_error_code and nil ~= error_code_info.error_code_desc and error_code_info.error_code_desc ~= "" then
            error_code_desc = error_code_info.error_code_desc;  -- 上面的服务只能查到平台侧定义的错误码信息
        end

        local error_data_info = comm_center.call_service("i_service_get_error_info_cfg", { game_id = game_id, error_code = error_code });
        if nil == error_data_info or nil == error_data_info.info then
            base_utility.log("[poptips_control][check_need_feedback]no info");
            return param;
        end

        local error_data = base_utility.json_to_table(error_data_info.info);
        if nil == error_data then
            base_utility.log("[poptips_control][check_need_feedback]no error data");
            return param;
        end

        base_utility.log("[poptips_control][check_need_feedback]auto feed back : " ..
            tostring(error_data.auto_feedback));
        if error_data.auto_feedback then
            base_utility.log("[poptips_control][check_need_feedback]need auto feedback");
            param["cbx_dont_ask"] = {
                visible = true;
                text = LoadStr("ERROR_CODE_FEEDBACK_CHECKBOX_TIPS");
                checked = true;
            };
            -- 提交反馈
            local feedbackHandle = function(uilib, ctr_id)
                base_utility.log("[poptips_control]feedbackHandle")
                if MainCommon.use_navtive(param.native) then
                    local btn_group_id = ui_utility.get_ctrl_parent_id(ctr_id);
                    local win_id = ui_utility.get_ctrl_parent_id(btn_group_id);
                    local cbx_btn_id = ui_utility.get_ctrl_id_by_path(win_id, "Container_1\\cbx_dont_ask");
                    local cbx_btn = ui_utility.get_ctrl_ptr_by_id(cbx_btn_id);
                    if nil == cbx_btn or cbx_btn:IsChecked() == FALSE then
                        base_utility.log("[poptips_control][feedbackHandle]no chkbox",
                                true)
                        return; -- 没有勾选，直接返回
                    end
                end

                local upload_info = {};
                local json_info = {};
                json_info.appid = "88001";
                json_info.moduleid = error_data_info.game_id;
                local modulename = "--";
                local game_info = banner_model.get_game_base_info(error_data_info.game_id);
                if nil ~= game_info and nil ~= game_info.name and game_info.name ~= "" then
                    modulename = game_info.name;
                end
                json_info.modulename = modulename;
                json_info.message = error_code.."-"..error_code_desc;
                upload_info.json = base_utility.table_to_json(json_info);
                upload_info.error_code = error_code;
                upload_info.log_paths = {};

                base_utility.log("[poptips_control] check_need_feedback error_code:" ..error_code);
                base_utility.log("[poptips_control] check_need_feedback upload_info.json:" ..upload_info.json);
                if error_data.auto_upload_log and nil ~= error_data.log_paths and error_data.log_paths ~= "" then
                    if nil ~= error_data.log_paths and table.getn(error_data.log_paths) > 0 then
                        upload_info.log_paths = error_data.log_paths;
                    end
                end
                for i =1,  #upload_info.log_paths do
                    base_utility.log("[poptips_control] check_need_feedback log_paths1:" ..tostring(upload_info.log_paths[i]));
                end

                local log_path_num = table.getn(upload_info.log_paths);
                local gen_mem_logs = comm_center.call_service("i_service_gen_memory_log", { error_code = error_code, scene = "platform", pre_path = "\\memlog", log_path = "" });
                local add_mem_logs = comm_center.call_service("i_service_add_mem_log", { error_code_id = error_code });
                if nil ~= add_mem_logs and nil ~= add_mem_logs.add_logs then
                    local add_logs = base_utility.json_to_table(add_mem_logs.add_logs);
                    if type(add_logs) == "table" and nil ~= add_logs.add_logs then
                        for key, value in pairs(add_logs.add_logs) do
                            log_path_num = log_path_num + 1;
                            upload_info.log_paths[log_path_num] = value;
                        end
                    end
                end

                for i =1,  #upload_info.log_paths do
                    base_utility.log("[poptips_control] check_need_feedback log_paths2:" ..tostring(upload_info.log_paths[i]));
                end
                comm_center.call_service("i_service_feedback_upload_form", upload_info);
            end;
            if nil == param["okBtn"] then
                param["okBtn"] = {
                    visible = true;
                    text = LoadStr("ERROR_CODE_FEEDBACK_CONFIRM_TIPS");
                };
            end

            local orgHandle = param["okBtn"]["handle"];
            param["okBtn"]["handle"] = function(uilib, ctr_id, user_data, addon_info)
                base_utility.log("[poptips_control][check_need_feedback]ok handle");
                if true == addon_info.cbx_dont_ask_checked then
                    feedbackHandle(uilib, ctr_id);
                end
                if nil ~= orgHandle then
                    base_utility.log("[poptips_control][check_need_feedback]call org handle");
                    orgHandle(uilib, ctr_id, user_data, addon_info);
                end
            end;

        elseif error_data.log_file_count > 0 then
            -- 把log压缩成zip放在本地
            local zip_result = comm_center.call_service(
                    "i_service_zip_log_local", { log_file_count = error_data.log_file_count });
        end
        return param;
    end;

    show_tips = function(param)
        local opt = base_utility.copy_table_without_function(param);
        local table_json = base_utility.table_to_json(opt);
        base_utility.log("[poptips_control]show_tips, " .. table_json);

        if poptips_view:exist(table_json) then
            base_utility.log("[poptips_control][show_tips]already exist, discard:" ..
                    table_json, true);
            return;
        end

        if not param then
            param = {};
        end

        param.table_hash = table_json;

        -- 判断是否需要提示进行反馈
        param = poptips_control.check_need_feedback(param);

        if param.ui_file or
            param.layout_file or
            MainCommon.use_navtive(param.native) then
            return poptips_control.show_tips_imple(param);
        end

        -- 默认web形式
        default_web_poptips.show(param);
    end;

    show_tips_imple = function(param)
        base_utility.log("[poptips_control] show_tips_imple");
        if not param then
            param = {};
        end
        local tips_mgr = poptips_model.get_tips_mgr();
        if nil == tips_mgr then
            base_utility.log("[poptips_control][show_tips_imple]tips_mgr is nil");
            return;
        end;

        local resolution_x, resolution_y, work_area_x, work_area_y = 
            ui_utility.get_screen_resolution();

        param['swidth'] = work_area_x;
        param['sheight'] = work_area_y;
        param['sx'] = 0;
        param['sy'] = 0;
        if param.check_game_state == nil or param.check_game_state == false then
            local win_id = poptips_control.ready_to_show_tips(param);
            return win_id;
        else -- 需要判断游戏状态
            if poptips_model.game_running_state == 0 then
                local win_id = poptips_control.ready_to_show_tips(param);
                return win_id;
            else -- 保存tips
                if poptips_model.game_running_state > 0  then
                    if poptips_model.waiting_common_tips_count >= 3 then
                        poptips_model.waiting_common_tips_list[poptips_model.waiting_common_tips_count + 1] = param;
                        for key, value in pairs(poptips_model.waiting_common_tips_list) do
                            if  poptips_model.waiting_common_tips_list[key + 1] ~= nil then
                                poptips_model.waiting_common_tips_list[key]
                                    = poptips_model.waiting_common_tips_list[key + 1];
                            end
                        end
                        poptips_model.waiting_common_tips_list[poptips_model.waiting_common_tips_count + 1] = nil;
                    else
                        poptips_model.waiting_common_tips_count = poptips_model.waiting_common_tips_count + 1;
                        local waiting_common_tips_count = poptips_model.waiting_common_tips_count;
                        poptips_model.waiting_common_tips_list[waiting_common_tips_count] = param;
                    end
                    base_utility.log("[poptips_control][show_tips] save tips "
                        .. poptips_model.waiting_common_tips_count);
                end
            end
        end
    end;

    show_saved_common_tips = function (info_id, time_stamp)
        base_utility.log("[poptips_control][show_saved_commmon_tips] try to reshow login tips, info_id="
            .. info_id .. " game_running_state=" .. poptips_model.game_running_state);
        if poptips_model.game_running_state == 0 then
            local win_id = poptips_control.ready_to_show_tips(poptips_model.waiting_common_tips_list[info_id]); -- 重新弹出
            base_utility.log("[poptips_control][show_saved_common_tips] reshow tips");
            if info_id >= poptips_model.waiting_common_tips_count then
                poptips_model.waiting_common_tips_list = {};
                poptips_model.waiting_common_tips_count = 0;
            end
        end
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            tpfui:RemoveInterval("poptips_control.show_saved_common_tips", info_id);
        end
    end;

    game_ended_show_common_tips = function (event_id, param)
        local tips_count = table.getn(poptips_model.waiting_common_tips_list);
        if tips_count > 0 then
            base_utility.log("[poptips_control][game_ended_show_common_tips] waiting_common_tips_list, count:"
                .. tips_count);
            local tips_display_idx = 0;
            for key, value in pairs(poptips_model.waiting_common_tips_list) do
                if tips_display_idx >= 3 then -- 控制tips弹出量
                    return;
                end;
                local tpfui =
                    base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
                tpfui:AddInterval("poptips_control.show_saved_common_tips",
                    (tips_display_idx + 1) * 2000, key);
                base_utility.log("[poptips_control][game_ended_show_common_tips] inform reshow common tips, idx: "
                    .. tips_display_idx .. " key=" .. key);
                tips_display_idx = tips_display_idx + 1;
            end
        end
    end;

    ready_to_show_tips = function(param)
        base_utility.log("[poptips_control]ready_to_show_tips ");
        if poptips_control.is_big_picture_mode() then
            if param == nil or param.ignore_bp_ui == nil or param.ignore_bp_ui == true then
                -- 大屏模式下仍然展示
                return poptips_view:show(param);
            elseif param ~= nil and param.hold_bp_ui ~= nil and param.hold_bp_ui == true then
                -- 延迟到大屏退出以后展示
                poptips_control.add_to_bp_hold_tips_list(param);
            end
        else
            return poptips_view:show(param);
        end
    end;

    on_ui_mode_change = function(data)
        if data == nil or data.mode == nil then
            return;
        end
        if poptips_model.cur_ui_mode == data.mode then
            return;
        end
        poptips_model.cur_ui_mode = data.mode;
        if poptips_control.is_big_picture_mode() == false then
            -- 大屏模式退出以后，展示保存的tips
            poptips_control.consume_bp_hold_tips();
        end
    end;

    is_big_picture_mode = function()
        if poptips_model.cur_ui_mode == base_utility.CLIENT_UI_MODE.BIG_PICTURE_UI then
            return true;
        end
        return false;
    end;

    add_to_bp_hold_tips_list = function(param)
        if poptips_model.hold_in_bp_ui_tips_count >= 3 then
            poptips_model.hold_in_bp_ui_tips_list[poptips_model.hold_in_bp_ui_tips_count + 1] = param;
            for key, value in pairs(poptips_model.hold_in_bp_ui_tips_list) do
                if  poptips_model.hold_in_bp_ui_tips_list[key + 1] ~= nil then
                    poptips_model.hold_in_bp_ui_tips_list[key]
                        = poptips_model.hold_in_bp_ui_tips_list[key + 1];
                end
            end
            poptips_model.hold_in_bp_ui_tips_list[poptips_model.hold_in_bp_ui_tips_count + 1] = nil;
        else
            poptips_model.hold_in_bp_ui_tips_count = poptips_model.hold_in_bp_ui_tips_count + 1;
            poptips_model.hold_in_bp_ui_tips_list[poptips_model.hold_in_bp_ui_tips_count] = param;
        end
    end;

    consume_bp_hold_tips = function()
        local tips_count = table.getn(poptips_model.hold_in_bp_ui_tips_list);
        if tips_count > 0 then
            local tips_display_idx = 0;
            for key, value in pairs(poptips_model.hold_in_bp_ui_tips_list) do
                if tips_display_idx >= 3 then -- 控制tips弹出量
                    return;
                end;
                local tpfui =
                    base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
                tpfui:AddInterval("poptips_control.show_bp_hold_tips",
                    (tips_display_idx + 1) * 2000, key);
                tips_display_idx = tips_display_idx + 1;
            end
        end
    end;

    show_bp_hold_tips = function(info_id, time_stamp)
        local param = poptips_model.hold_in_bp_ui_tips_list[info_id];
        if param ~= nil and poptips_control.is_big_picture_mode() == false then
            if param.check_game_state == nil or 
               param.check_game_state == false or
              (param.check_game_state == true and poptips_model.game_running_state == 0) then
                local win_id = poptips_view:show(param);
                if info_id >= poptips_model.hold_in_bp_ui_tips_count then
                    poptips_model.hold_in_bp_ui_tips_list = {};
                    poptips_model.hold_in_bp_ui_tips_count = 0;
                end
            end
        end
        
        local tpfui =
            base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui ~= nil then
            tpfui:RemoveInterval("poptips_control.show_bp_hold_tips", info_id);
        end
    end;

    -- 显示公共web弹窗
    show_web = function(info)
        info.modal = false;
        if info.is_multi_win == nil then
            info.is_multi_win = true;
        end
        if not info.ui_file then
            info.ui_file = "webview.tpf";
        end
        info.layout_callback = "webview_view.layout";
        info.layout_file = "poptips\\webview_view.lua";
        return poptips_control.show_tips_imple(info);
    end;
    
    enable_ok_btn = function(wnd_id, enabled, reset_desc)
        poptips_view:enable_ok_btn(wnd_id, enabled, reset_desc);
    end;

    -- 本地web弹窗
    show_local_web_wnd = function(info)
        info.delay_show_wnd = true;
        if not info.ui_file then
            info.ui_file = "local_web_wnd.tpf";
        end
        if not info.layout_callback then
            info.layout_callback = "local_web_wnd.layout";
        end
        if not info.layout_file then
            info.layout_file = "local_web\\local_web_wnd.lua";
        end

        info.win_close_callback = poptips_control.on_win_close;

        return poptips_control.show_tips_imple(info);
    end;

    on_win_close = function(uilib, win_id)
        base_utility.log("[poptips_control]on_win_close " .. tostring(win_id));
        poptips_view:new_win_close(uilib, win_id);
    end
};

poptips_control.init();
